from flask import Flask
import config
from exts import db, mail,cache,csrf, avatars, jwt, cors
from flask_migrate import Migrate
from apps.front import front_bp
from bbs_celery import make_celery
from apps.media import media_bp
from apps.cmsapi import cmsapi_bp
import commands


# 将 ORM模型映射到数据库中三部曲
# 1. 初始化迁移仓库：  flask  db init
# 2. 将ORM模型生成迁移脚本： flask db migrate
# 3. 运行迁移脚本： flask db upgrade

app = Flask(__name__)
app.config.from_object(config)

#app.config['AVATARS_IDENTICON_ROWS'] = 5

#db.__init__app(app)
db.init_app(app)
mail.init_app(app)
cache.init_app(app)
csrf.init_app(app)
avatars.init_app(app)
jwt.init_app(app)
# 允许跨域
cors.init_app(app, resources={r'/cmsapi/*':{'origins': "*"}})

# 排除cmsapi的csrf验证
csrf.exempt(cmsapi_bp)

migrate = Migrate(app, db)

mycelery = make_celery(app)

# 注册蓝图
app.register_blueprint(front_bp)
app.register_blueprint(media_bp)
app.register_blueprint(cmsapi_bp)

#注册命令     flask init_boards
app.cli.command("init_boards")(commands.init_boards)
app.cli.command("init_roles")(commands.init_roles)
app.cli.command("init_developor")(commands.init_developor)
app.cli.command("bind_roles")(commands.bind_roles)
app.cli.command("create_test_posts")(commands.create_test_posts)
# @app.route('/')
# def hello_world():  # put application's code here
#     return 'Hello World!'


if __name__ == '__main__':
    app.run(debug=True,port=8989)
